package com.zjlanyun.erp.interior;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSON;
import com.yolanda.nohttp.rest.Response;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.nohttp.HttpRequest;
import com.zjlanyun.erp.nohttp.SimpleHttpListener;
import com.zjlanyun.erp.utils.Toastor;
import com.zjlanyun.erp.utils.ViewUtil;
import com.zjlanyun.erp.view.BaseActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;

public class SearchList extends BaseActivity {

    @Bind(R.id.lv) ListView lv;
    @Bind(R.id.at_keyword) AutoCompleteTextView at_keyword;
    @Bind(R.id.btn_search) Button btn_search;

    private ArrayList<HashMap<String, Object>> mList;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_list);
        initBaseView();
        initView();
    }

    private void initView(){
        title.setText("产品搜索");
        Intent intent = getIntent();
        if (intent.getStringExtra("keyword") != null) {
            at_keyword.setText(intent.getStringExtra("keyword"));
            getData();
        }


        //搜索
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("FItemID", Integer.valueOf(mList.get(position).get("FItemID").toString()));
                intent.putExtra("FName", mList.get(position).get("FName").toString());
                intent.putExtra("FNumber", mList.get(position).get("FNumber").toString());
                intent.putExtra("FModel", mList.get(position).get("FModel").toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void getData(){
        if (at_keyword.getText().toString().equals("")){
            Toastor.getInstance().showShortToast("请输入关键词");
            return;
        }
        ViewUtil.hideInputMethod(mContext, btn_search);
        mList = new ArrayList<>();
        simpleAdapter = new SimpleAdapter(mContext, mList, R.layout.search_list_item, new String[]{"FName", "FNumber", "FModel"}, new int[]{R.id.tv_FName, R.id.tv_FNumber, R.id.tv_FModel});
        lv.setAdapter(simpleAdapter);

        HashMap<String, Object> param = new HashMap<>();
        param.put("keyword", at_keyword.getText().toString());
        HttpRequest httpRequest = new HttpRequest(mContext, WebApi.production(), "getProductList", JSON.toJSONString(param), true, true);
        httpRequest.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                super.onSucceed(what, response);
                JSONArray list = response.get().optJSONObject("data").optJSONArray("list");
                for (int i = 0; i < list.length(); i++) {
                    JSONObject item = list.optJSONObject(i);
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("FName", item.optString("FName"));
                    map.put("FNumber", item.optString("FNumber"));
                    map.put("FModel", item.optString("FModel"));
                    map.put("FItemID", item.optInt("FItemID"));
                    mList.add(map);
                }
                simpleAdapter.notifyDataSetChanged();

                if (list.length() == 0) {
                    Toastor.getInstance().showShortToast("没有查询到匹配的产品");
                }
            }
        });
    }
}
